'use strict';

let puppeteer = require('puppeteer');

// (async() => {
//   let browser = await puppeteer.launch({
//     headless:false,
//   });
//   let browserWSEndpoint = browser.wsEndpoint();
//   // 从Chromium 断开连接
//   browser.disconnect();
//   browser = await puppeteer.connect({browserWSEndpoint})

//   const page = await browser.newPage();
//   await page.goto('https://www.baidu.com');
//   page.waitFor(2000)
//   browserWSEndpoint = browser.wsEndpoint();
//   browser.disconnect();

//   browser = await puppeteer.connect({browserWSEndpoint}) 
//   // const page2 = await browser.newPage();
//   await page.goto('https://www.163.com');
//   page.waitFor(5000)
//   browser.disconnect();
//   // await browser.close();
// })();

puppeteer.launch({headless:false}).then(async browser => {
  // Store the endpoint to be able to reconnect to Chromium
  const page = await browser.newPage()
  await page.goto('https://www.baidu.com')
  await page.waitFor(2000)
  const browserWSEndpoint = browser.wsEndpoint();
  // Disconnect puppeteer from Chromium
  browser.disconnect();

  // Use the endpoint to reestablish a connection
  let browser2 = await puppeteer.connect({browserWSEndpoint});
  const page2 = await browser2.newPage()
  await page2.goto('http://oldhouseinfo.fdc.com.cn/#/login')
  page2.close()

  browser2 = await puppeteer.connect({browserWSEndpoint});
  const page3 = await browser2.newPage()
  page3.on('console', msg => {
  	if (typeof msg === 'object') {
  		console.log(msg.text())
  	}else{
  		console.log(msg)
  	}
  })
  page3.once('load',() => console.log("page loaded !!!") )
  const p3 = await page3.goto('https://movie.douban.com/')
  
  await page3.waitFor(2000)
  const content = await p3.content()

  // const aHandle = await page3.evaluateHandle(() => document.body);
  // const resultHandle = await page3.evaluateHandle(body => body.innerHTML, aHandle);
  // console.log(await resultHandle.jsonValue());
  // await resultHandle.dispose();
  // await console.log(p3.headers())
  // await page3.waitFor(3000)
  // Close Chromium
  page3.close()
  // browser2.disconnect();
  
});